package com.ldj.struts.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ldj.hibernate.DataDictionary;
import com.ldj.hibernate.DataDictionaryDAO;
import com.ldj.hibernate.StatisticsDAO;

public class EmployerStatisticsAction extends DispatchAction {
	public ActionForward find(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("find");
	}
	
	public ActionForward xls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StatisticsDAO dao = new StatisticsDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String type = request.getParameter("type");
		List list = dao.findEmployerFromAreaCode(areaCode, type);
		request.setAttribute("list", list);
		return mapping.findForward("xls");
	}
	
	public ActionForward detail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StatisticsDAO dao = new StatisticsDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String type = request.getParameter("type");
		List list = dao.findEmployerFromAreaCodePage(areaCode, type);
		request.setAttribute("list", list);
		return mapping.findForward("detail");
	}
	
	public ActionForward pic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String areaCode = request.getParameter("areaCode");
		String type = request.getParameter("type");
		DataDictionaryDAO dao1 = new DataDictionaryDAO(request, response);
		DataDictionary dd = dao1.findFromCode(areaCode);
		StatisticsDAO dao = new StatisticsDAO(request, response);
		Hashtable rd = new Hashtable();
		String barTitle = dd.getDataDictionaryName();
		String pieTitle = dd.getDataDictionaryName();
		if("0".equals(type)){
			barTitle += "签定合同人数低于50%";
			pieTitle += "签定合同人数低于50%";
		}else if("1".equals(type)){
			barTitle += "投保人数低于50%";
			pieTitle += "投保人数低于50%";
		}
		barTitle += "的企业柱状图";
		pieTitle += "的企业饼图";
		rd.put("barTitle", barTitle);
		rd.put("pieTitle", pieTitle);
		rd.put("domain", "企业类型");
		rd.put("range", "企业数");
		List dList = new Vector();
		List list = dao.findEmployerCountFromAreaCode(areaCode, type);
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				Object[] obj = (Object[])list.get(i);
				Hashtable hd = new Hashtable();
				hd.put("name", obj[0]);
				hd.put("title", "企业数量");
				hd.put("value", obj[1]);
				dList.add(hd);
			}
		}
		rd.put("data", dList);
		request.getSession().setAttribute("data", rd);
		return mapping.findForward("pic");
	}
	
	public ActionForward annualFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String year=new SimpleDateFormat("yyyy").format(Calendar.getInstance().getTime());
		int y = Integer.parseInt(year); 
		int endYear = y + 2;
		request.setAttribute("endYear", endYear);
		request.setAttribute("nowYear", y);
		return mapping.findForward("find");
	}
	
	public ActionForward annualXls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StatisticsDAO dao = new StatisticsDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String year = request.getParameter("year");
		List list = dao.findEmployerAnnualFromAreaCode(areaCode, year);
		request.setAttribute("list", list);
		return mapping.findForward("xls");
	}
	
	public ActionForward annualDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StatisticsDAO dao = new StatisticsDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String year = request.getParameter("year");
		List list = dao.findEmployerAnnualFromAreaCodePage(areaCode, year);
		request.setAttribute("list", list);
		return mapping.findForward("detail");
	}
	
	public ActionForward annualPic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String areaCode = request.getParameter("areaCode");
		String year = request.getParameter("year");
		DataDictionaryDAO dao1 = new DataDictionaryDAO(request, response);
		DataDictionary dd = dao1.findFromCode(areaCode);
		StatisticsDAO dao = new StatisticsDAO(request, response);
		Hashtable rd = new Hashtable();
		String barTitle = year + "年" + dd.getDataDictionaryName();
		String pieTitle = year + "年" + dd.getDataDictionaryName();
		barTitle += "未参加年审企业柱状图";
		pieTitle += "未参加年审企业饼图";
		rd.put("barTitle", barTitle);
		rd.put("pieTitle", pieTitle);
		rd.put("domain", "企业类型");
		rd.put("range", "企业数");
		List dList = new Vector();
		List list = dao.findEmployerAnnualCountFromAreaCode(areaCode, year);
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				Object[] obj = (Object[])list.get(i);
				Hashtable hd = new Hashtable();
				hd.put("name", obj[0]);
				hd.put("title", "企业数量");
				hd.put("value", obj[1]);
				dList.add(hd);
			}
		}
		rd.put("data", dList);
		request.getSession().setAttribute("data", rd);
		return mapping.findForward("pic");
	}
	
	public ActionForward contractFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("find");
	}
	
	public ActionForward contractXls(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StatisticsDAO dao = new StatisticsDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String type = request.getParameter("type");
		List list = dao.findEmployerContractFromAreaCodePage(areaCode, type);
		request.setAttribute("list", list);
		return mapping.findForward("xls");
	}
	
	public ActionForward contractDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		StatisticsDAO dao = new StatisticsDAO(request, response);
		String areaCode = request.getParameter("areaCode");
		String type = request.getParameter("type");
		List list = dao.findEmployerContractFromAreaCodePage(areaCode, type);
		request.setAttribute("list", list);
		return mapping.findForward("detail");
	}
	
	public ActionForward contractPic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String areaCode = request.getParameter("areaCode");
		String year = request.getParameter("year");
		DataDictionaryDAO dao1 = new DataDictionaryDAO(request, response);
		DataDictionary dd = dao1.findFromCode(areaCode);
		StatisticsDAO dao = new StatisticsDAO(request, response);
		Hashtable rd = new Hashtable();
		String barTitle = year + "年" + dd.getDataDictionaryName();
		String pieTitle = year + "年" + dd.getDataDictionaryName();
		barTitle += "未参加年审企业柱状图";
		pieTitle += "未参加年审企业饼图";
		rd.put("barTitle", barTitle);
		rd.put("pieTitle", pieTitle);
		rd.put("domain", "企业类型");
		rd.put("range", "企业数");
		List dList = new Vector();
		List list = dao.findEmployerAnnualCountFromAreaCode(areaCode, year);
		if(list != null && list.size() > 0){
			for(int i = 0; i < list.size(); i ++){
				Object[] obj = (Object[])list.get(i);
				Hashtable hd = new Hashtable();
				hd.put("name", obj[0]);
				hd.put("title", "企业数量");
				hd.put("value", obj[1]);
				dList.add(hd);
			}
		}
		rd.put("data", dList);
		request.getSession().setAttribute("data", rd);
		return mapping.findForward("pic");
	}
	
}